import * as $util from '/lib/hc-util.js'

var refercontrol = {
    props: {
        definition: {
            type: Object,
            default () {
                return {};
            }
        }
    },
    created: function() {
        this.$set(this.definition, 'settingList', $pinfo.ruleList.filter(it => it.rtype === 'data:strategy'));
    },
    watch: {
        "definition.refer": {
            immediate: true,
            handler: function(val, old) {
                if (!val) {
                    this.$set(this.definition, 'inputList', []);
                    this.$set(this.definition, 'resultStruct', null);
                    this.$set(this.definition, 'resultMap', {});
                    return;
                };
                
                let strategyDef = $pinfo.ruleList.find(it => it.rpath == this.definition.refer);
                let lst = strategyDef ? strategyDef.list : [];
                $util.$tree.traverse(lst, (it) => it.label = it.name ? it.code + ':' + it.name : it.code);
                
                this.$set(this.definition, 'inputList', lst);
                
                let structRoot = $util.clone($config.componentMap['data:strategy'].resultStruct.root);
                let valueListStruct = structRoot.list[structRoot.list.length - 1];
                strategyDef.valueDefList.forEach(it => {
                    valueListStruct.list.push({
                        id: it.id,
                        code: it.code,
                        name: it.name,
                        type: it.dataType,
                        important: 9,
                    })
                });
                
                this.$set(this.definition, 'resultStruct', structRoot);
                if (this.definition.resultStruct) {
                    $util.$tree.sort(this.definition.resultStruct.list,(a, b) => b.important - a.important);
                }
                this.$set(this.definition, 'resultMap', {});
            },
        },
    },
    methods: {
        handleReferChange: function(val, old) {
        },
    },
    template: `
    <el-form-item label="策略场景" prop='refer' :rules="[{
    				required: true,
    				message: '请选择使用策略的业务场景',
    				trigger: 'change'
    			}
    		]">
    
       <el-cascader style='width: 100%;'
            :props="{label:'rname',value:'rpath',children:'null',expandTrigger:'hover',emitPath:false}"
            v-model='definition.refer' placeholder="关键字搜索" :options="definition.settingList"
            filterable @change="handleReferChange">
        </el-cascader>
    </el-form-item>
   `,
};

var inputcontrol = {
    props: {
        value: {
            type: [Number],
            default: 0
        },
        definition: {
            type: Object,
            default () {
                return {};
            }
        }
    },
    data: function() {
        return {
            inputValue: null,
        }
    },
    created: function() {
        this.inputValue = this.value;
        if (!this.definition.inputList) {
            let encoder = $pinfo.ruleList.find(it => it.rpath == this.definition.refer);
            let lst = encoder ? encoder.list : [];
            $util.$tree.traverse(lst, (it) => it.label = it.name ? it.code + ':' + it.name : it.code);

            this.$set(this.definition, 'inputList', lst)
        }
    },
    methods: {
        handleInputChange: function(val) {
            this.$emit('input', val);
        }
    },
    template: `
        <el-cascader style='width: 100%;'
            :props="{label:'label',value:'id',children:'list',expandTrigger:'hover',emitPath:false}"
            v-model='inputValue' placeholder="可搜索" :options="definition.inputList"
            filterable clearable @change="handleInputChange">
        </el-cascader>
        `
};

var actionconfig = {
    code: 'StrategySetter',
    name: '业务策略引用',
    refer: refercontrol,
    input: inputcontrol,
    // map: {},
};

export default actionconfig;
